#pragma once

#include <string>
#include <chrono>
#include "order.h"

namespace hft {

// 持仓结构
struct Position {
    std::string symbol;                          // 合约代码
    PositionSide side = PositionSide::Net;       // 持仓方向
    int volume = 0;                              // 持仓数量
    double avg_price = 0.0;                      // 平均持仓价格
    double last_price = 0.0;                     // 最新价格
    double unrealized_pnl = 0.0;                 // 未实现盈亏
    double realized_pnl = 0.0;                   // 已实现盈亏
    double margin = 0.0;                         // 占用保证金
    double margin_ratio = 0.0;                   // 保证金率
    std::string strategy_id;                     // 策略ID
    std::string account;                         // 账户
    std::string exchange;                        // 交易所
    
    // 今日持仓统计
    int today_volume = 0;                        // 今仓数量
    double today_avg_price = 0.0;                // 今仓平均价格
    
    // 历史持仓统计
    int history_volume = 0;                      // 昨仓数量
    double history_avg_price = 0.0;              // 昨仓平均价格
    
    // 开平仓统计
    int open_volume = 0;                         // 开仓数量
    double open_amount = 0.0;                    // 开仓金额
    int close_volume = 0;                        // 平仓数量
    double close_amount = 0.0;                   // 平仓金额
    
    // 风险指标
    double leverage = 0.0;                       // 杠杆率
    double position_value = 0.0;                 // 持仓市值
    double max_drawdown = 0.0;                   // 最大回撤
    
    // 时间戳
    std::chrono::system_clock::time_point create_time = std::chrono::system_clock::now();  // 创建时间
    std::chrono::system_clock::time_point update_time = std::chrono::system_clock::now();  // 更新时间
    
    // 更新持仓
    void updatePosition(const Trade& trade) {
        update_time = std::chrono::system_clock::now();
        
        // 更新持仓数量和方向
        if (trade.side == OrderSide::Buy) {
            if (volume >= 0) {
                // 多头加仓
                double total_value = volume * avg_price + trade.volume * trade.price;
                volume += trade.volume;
                avg_price = total_value / volume;
                side = PositionSide::Long;
            } else {
                // 空头减仓
                volume += trade.volume;
                if (volume >= 0) {
                    // 转为多头
                    side = PositionSide::Long;
                    avg_price = trade.price;
                }
            }
        } else {
            if (volume <= 0) {
                // 空头加仓
                double total_value = std::abs(volume) * avg_price + trade.volume * trade.price;
                volume -= trade.volume;
                avg_price = total_value / std::abs(volume);
                side = PositionSide::Short;
            } else {
                // 多头减仓
                volume -= trade.volume;
                if (volume <= 0) {
                    // 转为空头
                    side = PositionSide::Short;
                    avg_price = trade.price;
                }
            }
        }
        
        // 更新今仓/昨仓
        if (std::chrono::system_clock::to_time_t(trade.trade_time) > 
            std::chrono::system_clock::to_time_t(create_time)) {
            today_volume = std::abs(volume);
            today_avg_price = avg_price;
        } else {
            history_volume = std::abs(volume);
            history_avg_price = avg_price;
        }
        
        // 更新开平仓统计
        if (trade.side == OrderSide::Buy) {
            open_volume += trade.volume;
            open_amount += trade.volume * trade.price;
        } else {
            close_volume += trade.volume;
            close_amount += trade.volume * trade.price;
        }
        
        // 更新盈亏
        if (last_price > 0) {
            calculateUnrealizedPnL();
        }
    }
    
    // 计算未实现盈亏
    void calculateUnrealizedPnL() {
        if (volume == 0 || last_price <= 0) {
            unrealized_pnl = 0;
            return;
        }
        
        if (side == PositionSide::Long) {
            unrealized_pnl = volume * (last_price - avg_price);
        } else {
            unrealized_pnl = volume * (avg_price - last_price);
        }
        
        // 更新持仓市值
        position_value = std::abs(volume) * last_price;
        
        // 更新最大回撤
        double current_drawdown = unrealized_pnl / position_value;
        if (current_drawdown < max_drawdown) {
            max_drawdown = current_drawdown;
        }
    }
    
    // 更新最新价格
    void updateLastPrice(double price) {
        if (price <= 0) return;
        last_price = price;
        calculateUnrealizedPnL();
    }
    
    // 更新保证金
    void updateMargin(double margin_ratio) {
        if (margin_ratio <= 0) return;
        this->margin_ratio = margin_ratio;
        margin = position_value * margin_ratio;
        if (margin > 0) {
            leverage = position_value / margin;
        }
    }
};

} // namespace hft